package com.craiditx.preproccess;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JieBaHiveData_3 {
    /**
     * 读取excel中标签数据
     */
    public List<Map<String,String>> getExcel() throws Exception{
        InputStream inputStream=this.getClass().getResourceAsStream("/trade_mapping_setting.xlsx");
        XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        List<Map<String,String>> excel_data=new ArrayList<>();
        for(int i=1;i<sheetAt.getPhysicalNumberOfRows();i++){
            XSSFRow row = sheetAt.getRow(i);
            Iterator<Cell> cellIterator = row.cellIterator();
            Map<String,String> cell_map=new HashMap<>();
            int count=1;
            while(cellIterator.hasNext()){
                Cell next = cellIterator.next();
                if(next.toString()==""){cell_map.put("lev"+count,"null");}
                else{cell_map.put("lev"+count,next.toString());}
                count++;
            }
            excel_data.add(cell_map);
        }
        inputStream.close();
        return excel_data;
    }

    /**
     *关键字与标签的映射
     */
    public static List<Map<String,String>> mappingFeature(String[] inputWords) throws Exception {
        List<Map<String, String>> excel = new JieBaHiveData_3().getExcel();
        List<Map<String,String>> mapping_r=new ArrayList<>();
        if(inputWords.length>0){
            for (Map<String, String> map : excel) {
                for(int i=0;i<inputWords.length;i++){
                    if(inputWords[i].length()>0){
                        if(map.get("lev4").contains(inputWords[i])){
                            mapping_r.add(map);
                            break;
                        }else if(map.get("lev5").contains(inputWords[i])){
                            mapping_r.add(map);
                            break;
                        }
                    }
                }
            }
        }
        return mapping_r;
    }

    /**
     * 通过配置文件获取配置信息
     */
    public Properties readProperty() throws Exception{
        Properties proper=new Properties();
        InputStream inputStream=this.getClass().getResourceAsStream("/application.properties");
        InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"UTF-8");
        proper.load(inputStreamReader);
        inputStream.close();
        return proper;
    }

    /**
     * 将集合转化为自定义拼接符字符串
     */
    public static String mkString(List<String> words, String mk){
        StringBuffer buffer=new StringBuffer();
        for (int i=0;i<words.size();i++) {
            if(i==words.size()-1) buffer.append(words.get(i));
            else buffer.append(words.get(i)+mk);
        }
        return buffer.toString();
    }

    /**
     * 数据清洗，正则清洗掉无用字符
     */
    public static String clearData(String data,String regex){
        String resultStr=data;
        Matcher matcher = Pattern.compile(regex).matcher(data);
        while(matcher.find()){
            String group = matcher.group();
            resultStr=resultStr.replace(group,"");
        }
        return resultStr;
    }

    /**
     *正则匹配所需字符串或词
     */
    public static List<String> contData(String data,String regex){
        Matcher matcher = Pattern.compile(regex).matcher(data);
        List<String> list=new ArrayList<>();
        while(matcher.find()){
            list.add(matcher.group());
        };
        return list;
    }



    public static void main(String[] args) throws Exception {
        String str=clearData("古井烧鹅王（群星店）","[^\\u4e00-\\u9fa5\\d\\w\\-·[（][）][(][)]]|_");
        System.out.println(str);
        //以字方式提取特征
//        System.out.println(joinFeature(str.split("")));
//        //以词方式提取特征
//        System.out.println(joinFeature(JieBaData(str).split(",")));
//        //以字方式映射标签
//        System.out.println(mappingFeature(str.split("")));
//        //以词方式映射标签+
//        System.out.println(mappingFeature(JieBaData(str).split(",")));
//        String[] arr={"src/main/resources/models92mc71t.crfsuite","中国人寿"};
//        UDFTag.main(arr);
//        System.out.println(mappingFeature("湘天下,米粉".split(",")));
//        System.out.println(mappingFeature("湘天下,米粉".split(",")).size());
//        System.out.println(readProperty().getProperty("regex"));
        System.out.println(new JieBaHiveData_3().readProperty().getProperty("regex"));
        List<Map<String, String>> mf_word_list = mappingFeature(mkString(contData("便利店" , new JieBaHiveData_3().readProperty().getProperty("regex")), ",").split(","));
        // 行数是list的长度
        int rowNum1 = mf_word_list.size();
        for (int i = 0; i < rowNum1; i++) {
            String lev1 = mf_word_list.get(i).get("lev1");
            String lev2 = mf_word_list.get(i).get("lev2");
            String lev3 = mf_word_list.get(i).get("lev3");
            String[] resultSet = new String[3];
            resultSet[0] = lev1;
            resultSet[1] = lev2;
            resultSet[2] = lev3;
            System.out.println(resultSet[0]+resultSet[1]+resultSet[2]);
        }
    }
}

